<template>
  <page-header-wrapper>
    <global-search :loading="searchLoading" :list="searchList" @search="handleSearch" @reset="handleReset">
    </global-search>
    <a-card :bordered="false">
      <s-table
        ref="table"
        size="default"
        rowKey="ID"
        :columns="columns"
        :data="loadData"
        :pagination="{ showTotal: (total) => `共 ${total} 条` }"
      >
        <template #tabAction>
          <y-button type="primary" icon="plus" @click="handleAdd">添加项目</y-button>
        </template>
        <span slot="index" slot-scope="text, record, index">{{ index + 1 }} </span>
        <ellipsis :length="20" tooltip slot="ellipsis" slot-scope="text">{{ text || '/' }}</ellipsis>
        <span slot="time" slot-scope="text">{{ text | moment('YYYY-MM-DD') }}</span>
        <div slot="action" slot-scope="text, record">
          <y-button type="link" @click="handleDetail(record)">查看</y-button>
          <y-button type="link" @click="handleSchedule(record)">查看进度</y-button>
          <a-dropdown :trigger="['click']">
            <span class="text-primary m-l-10">
              更多
              <a-icon type="down" />
            </span>
            <a-menu slot="overlay" @click="handleMenuClick($event, record)">
              <a-menu-item key="edit">
                <span type="link">修改</span>
              </a-menu-item>
              <a-menu-item key="delete">
                <span type="link">删除</span>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </div>
      </s-table>
    </a-card>
    <detail :visible.sync="visibleDetail" :data.sync="editData" />
    <engineeringForm :visible.sync="visibleForm" :data.sync="editData" @success="tableRefresh" />
  </page-header-wrapper>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import detail from './components/detail.vue'
import engineeringForm from './components/form.vue'
import { getProjectList, getPlanOrProjetOptions, postProject } from '@/api/followUpManagement'
export default {
  components: {
    STable,
    Ellipsis,
    detail,
    engineeringForm
  },
  data () {
    return {
      visibleDetail: false,
      visibleForm: false,
      editData: {},
      searchLoading: false,
      queryParam: {
        Userid: this.$store.getters.userid,
        strContent: {}
      },
      searchList: [
        {
          label: '所属计划',
          fields: 'JHName',
          type: 'select',
          props: {
            placeholder: '请选择所属计划',
            options: [],
            showSearch: true,
            optionFilterProp: 'children'
          }
        },
        {
          label: '项目名称',
          fields: 'Name',
          type: 'input',
          props: {
            placeholder: '请输入项目名称'
          }
        }
      ],
      columns: [
        {
          title: '序号',
          align: 'center',
          width: '80px',
          scopedSlots: { customRender: 'index' }
        },
        {
          title: '项目名称',
          align: 'center',
          dataIndex: 'Name',
          scopedSlots: { customRender: 'ellipsis' }
        },
        {
          title: '所属计划',
          align: 'center',
          dataIndex: 'JHName',
          scopedSlots: { customRender: 'ellipsis' }
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'Status',
          customRender: (text) => {
            const colorMap = new Map([
              ['已竣工', 'text-success'],
              ['缓建', 'text-warning'],
              ['在建', 'text-primary']
            ])
            return <span class={colorMap.get(text || '在建')}>{text || '在建'}</span>
          }
        },
        {
          title: '开发商',
          align: 'center',
          dataIndex: 'DLID',
          scopedSlots: { customRender: 'ellipsis' }
        },
        {
          title: '描述',
          align: 'center',
          dataIndex: 'Descr',
          scopedSlots: { customRender: 'ellipsis' }
        },
        {
          title: '记录日期',
          align: 'center',
          dataIndex: 'CreateDate',
          scopedSlots: { customRender: 'time' }
        },
        {
          title: '操作',
          align: 'center',
          width: '190px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getProjectList(requestParameters).then((res) => res)
      }
    }
  },
  created () {
    this.fetchData()
  },
  methods: {
    fetchData () {
      this.searchLoading = true
      getPlanOrProjetOptions({
        mode: 'JH'
      })
        .then((res) => {
          this.searchList[0].props.options = res.map((e) => {
            return {
              value: e.ID,
              label: e.NAME
            }
          })
        })
        .finally(() => {
          this.searchLoading = false
        })
    },
    handleSearch (values) {
      this.queryParam.strContent = values
      this.tableRefresh(true)
    },
    handleReset () {
      this.queryParam.strContent = {}
      this.tableRefresh(true)
    },
    tableRefresh (refresh = false) {
      this.$refs.table.refresh(refresh)
    },
    handleDetail (record) {
      this.editData = record
      this.visibleDetail = true
    },
    handleSchedule (record) {
      this.$router.push({
        name: 'engineeringProjectschedule',
        params: {
          id: record.ID
        },
        query: {
          name: record.Name
        }
      })
    },
    handleMenuClick (e, record) {
      const { key } = e
      if (key === 'edit') {
        this.editData = record
        this.visibleForm = true
      }
      if (key === 'delete') {
        this.$confirm({
          content: '确认删除此项目？',
          okText: '确认',
          cancelText: '取消',
          onOk: () => {
            return postProject({
              State: 'delete',
              project: {
                ID: record.ID
              }
            }).then((res) => {
              this.$message.success('删除成功！')
              this.tableRefresh()
            })
          }
        })
      }
    },
    handleAdd () {
      this.editData = {}
      this.visibleForm = true
    }
  }
}
</script>
<style scoped>
</style>
